{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_custom_output:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When running the code, you might have seen some printouts (when using `verbose=True`), which might or might not have made a lot of sense to you. Below, a quick summary of possible printouts you might encounter is provided."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. csv-table:: Types of Output\n",
    "   :header: \"Name\", \"Description\"\n",
    "   :widths: 5, 30\n",
    "\n",
    "   **n_gen**, \"The current number of generations or iterations until this point.\"\n",
    "   **n_eval**, \"The number of function evaluations so far.\"\n",
    "   **n_nds**, \"For multi-objective problems, the number of *non-dominated* solutions of the optima found.\"\n",
    "   **cv (min)**,  \"The minimum constraint violation (CV) in the current population\"\n",
    "   **cv (avg)**,  \"The average constraint violation (CV) of the current population\"\n",
    "   **f_opt**,  \"For single-objective problems, the best function value found so far.\"\n",
    "   **f_gap**,  \"For single-objective problems, the best gap to the optimum (only printed if the optimum is *known*).\"\n",
    "   **eps/indicator**, \"For multi-objective problems, the change of the indicator (ideal, nadir, f) over the last few generations (only printed if the Pareto-front is *unknown*). For more information we encourage you to have a look at the corresponding publication (:cite:`running`, `pdf <https://www.egr.msu.edu/~kdeb/papers/c2020003.pdf>`_).\"\n",
    "   **igd/gd/hv**, \"For multi-objective problems, the performance indicator (only printed if the Pareto-front is *known*).\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The default printouts can vary from algorithm to algorithm and from problem to problem. The type of printout is based on an implementation of the `Display` object. If you like to customize the output, you can also write your own, as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from pymoo.algorithms.moo.nsga2 import NSGA2\n",
    "from pymoo.problems import get_problem\n",
    "from pymoo.optimize import minimize\n",
    "from pymoo.util.display.column import Column\n",
    "from pymoo.util.display.output import Output\n",
    "\n",
    "\n",
    "class MyOutput(Output):\n",
    "\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.x_mean = Column(\"x_mean\", width=13)\n",
    "        self.x_std = Column(\"x_std\", width=13)\n",
    "        self.columns += [self.x_mean, self.x_std]\n",
    "\n",
    "    def update(self, algorithm):\n",
    "        super().update(algorithm)\n",
    "        self.x_mean.set(np.mean(algorithm.pop.get(\"X\")))\n",
    "        self.x_std.set(np.std(algorithm.pop.get(\"X\")))\n",
    "\n",
    "\n",
    "problem = get_problem(\"zdt2\")\n",
    "\n",
    "algorithm = NSGA2(pop_size=100)\n",
    "\n",
    "res = minimize(problem,\n",
    "               algorithm,\n",
    "               ('n_gen', 10),\n",
    "               seed=1,\n",
    "               output=MyOutput(),\n",
    "               verbose=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You have to inherit your custom display `MyDisplay` from the `Display` class for your own printout logic.\n",
    "The `_do` function will be called in each iteration, and the `Problem`, `Evaluator` and `Algorithm` are provided to you. For each column, you can add an entry to `self.output`, which will be formatted and then printed."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
